package com.example.translateappjava.Activity.HomeFragmentActivity.Forum;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.translateappjava.R;

public class PostDetailActivity extends AppCompatActivity {

    private TextView tvTitle, tvUsername, tvTime, tvContent, tvCategory;
    private TextView tvLikeCount, tvCommentCount;
    private ImageView ivAvatar, ivLike, ivComment, ivShare;
    private View btnLike, btnComment, btnShare;

    private String postTitle, postContent, postUsername, postTime, postCategory;
    private int likeCount, commentCount;
    private boolean isLiked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_detail);

        // 设置工具栏
        setupToolbar();

        // 获取传递的数据
        getIntentData();

        // 初始化视图
        initViews();

        // 设置数据
        setupData();

        // 设置点击事件
        setupClickListeners();
    }

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setTitle("帖子详情");
        }
    }

    private void getIntentData() {
        postTitle = getIntent().getStringExtra("post_title");
        postContent = getIntent().getStringExtra("post_content");
        postUsername = getIntent().getStringExtra("post_username");
        postTime = getIntent().getStringExtra("post_time");
        postCategory = getIntent().getStringExtra("post_category");
        likeCount = getIntent().getIntExtra("post_like_count", 0);
        commentCount = getIntent().getIntExtra("post_comment_count", 0);
    }

    private void initViews() {
        ivAvatar = findViewById(R.id.iv_avatar);
        tvUsername = findViewById(R.id.tv_username);
        tvTime = findViewById(R.id.tv_time);
        tvTitle = findViewById(R.id.tv_title);
        tvContent = findViewById(R.id.tv_content);
        tvCategory = findViewById(R.id.tv_category);

        tvLikeCount = findViewById(R.id.tv_like_count);
        tvCommentCount = findViewById(R.id.tv_comment_count);

        btnLike = findViewById(R.id.btn_like);
        btnComment = findViewById(R.id.btn_comment);
        btnShare = findViewById(R.id.btn_share);

        ivLike = findViewById(R.id.iv_like);
        ivComment = findViewById(R.id.iv_comment);
        ivShare = findViewById(R.id.iv_share);
    }

    private void setupData() {
        tvTitle.setText(postTitle);
        tvContent.setText(postContent);
        tvUsername.setText(postUsername);
        tvTime.setText(postTime);
        tvCategory.setText(postCategory);
        tvLikeCount.setText(String.valueOf(likeCount));
        tvCommentCount.setText(String.valueOf(commentCount));

        // 设置默认头像
        ivAvatar.setImageResource(R.drawable.default_avatar);
    }

    private void setupClickListeners() {
        // 点赞按钮
        btnLike.setOnClickListener(v -> {
            isLiked = !isLiked;
            if (isLiked) {
                likeCount++;
                ivLike.setImageResource(R.drawable.ic_like_filled);
                Toast.makeText(this, "已点赞", Toast.LENGTH_SHORT).show();
            } else {
                likeCount--;
                ivLike.setImageResource(R.drawable.ic_like);
                Toast.makeText(this, "已取消点赞", Toast.LENGTH_SHORT).show();
            }
            tvLikeCount.setText(String.valueOf(likeCount));
        });

        // 评论按钮
        btnComment.setOnClickListener(v -> {
            Toast.makeText(this, "评论功能开发中", Toast.LENGTH_SHORT).show();
            // 这里可以打开评论页面或弹出评论框
        });

        // 分享按钮
        btnShare.setOnClickListener(v -> {
            Toast.makeText(this, "分享功能开发中", Toast.LENGTH_SHORT).show();
            // 这里可以实现分享功能
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void finish() {
        super.finish();
        // 添加退出动画
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }
}